package net.lcheng.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.lcheng.commons.utils.PageList;
import net.lcheng.dao.PProductMapper;
import net.lcheng.model.PProduct;
import net.lcheng.model.PProductExample;
import net.lcheng.service.PProductService;
import net.lcheng.vo.PProductModel;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by zwr on 2016/7/6 0006.
 */
@Service
public class PProductServiceImpl implements PProductService {
    @Resource
    PProductMapper mapper;

    @Override
    public PageList<PProductModel> getPProductList(Integer pageIndex, Integer pageSize, String productName, String shopName, Date sdate, Date edate) {
        PageHelper.startPage(pageIndex, pageSize);
        PProductExample example = new PProductExample();
//
//        PProductExample.Criteria criteria = example.createCriteria();
//        if(StringUtils.isEmpty(productName) == false) {
//            criteria.andProductNameLike(productName);
//        }
//        if(start != null) {
//            criteria.andCreateOnBetween(start,end);
//        }
//        example.or(criteria);
//        example.setOrderByClause("product_id desc");

        List<PProductModel> list = mapper.getPProductList(productName, shopName, sdate, edate);
        PageInfo pageInfo = new PageInfo(list);
        PageList pageList = new PageList<PProductModel>();
        pageList.list = pageInfo.getList();
        pageList.totalCount = pageInfo.getTotal();
        pageList.totalPage = pageInfo.getPages();
        pageList.pageIndex = pageInfo.getPageNum();
        pageList.pageSize = pageInfo.getPageSize();
        return pageList;
    }

    @Override
    public int getPProductReIsShopRecommend(String productId) {
        PProduct pProduct = mapper.selectByPrimaryKey(productId);
        if (pProduct != null) {
            PProduct PProductNew = new PProduct();
            PProductNew.setIsShopRecommend(1 - pProduct.getIsShopRecommend());
            PProductNew.setProductId(productId);
            mapper.updateByPrimaryKeySelective(PProductNew);
            return 1;
        }
        return -1;
    }

    @Override
    public int getPProductByStatus(String id, Integer status) {
        PProduct pProduct = new PProduct();
        pProduct.setProductId(id);
        pProduct.setStatus(status);
        return mapper.updateByPrimaryKeySelective(pProduct);
    }

    @Override
    public PProduct pProductList() {
        PProductExample example = new PProductExample();
        for (int i = 0; i < 3; i++) {
            example.or().andStatusEqualTo(i);
        }
        List<PProduct> list = mapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    /***
     * 获取店铺id(店铺首页显示)
     *
     * @param shopId
     * @return
     */
    @Override
    public List<PProduct> getPProductByShopId(Integer shopId) {
        PProductExample example = new PProductExample();
        example.or().andWxShopShopIdEqualTo(shopId).andStatusEqualTo(1);
        return mapper.selectByExample(example);
    }
    @Override
    public List<PProduct> getPProductBySaleCountTotal(Integer saleCountTotal,Integer shopId) {
        List<PProduct> list = mapper.getPProductBySaleCountTotal(saleCountTotal,shopId);
        return list;
    }
    @Override
    public int addProduct(String productId, String pCategoryCategoryId, Integer wxShopShopId, String wxShopCategoryId, String productCode, String productName, String productMainImage, BigDecimal salePrice, Integer inventoryNum, BigDecimal freight, String productDescription) {
        PProduct pProduct = new PProduct();
        pProduct.setProductId(productId);
        pProduct.setpCategoryCategoryId(pCategoryCategoryId);
        pProduct.setWxShopShopId(wxShopShopId);
        pProduct.setWxShopCategoryId(wxShopCategoryId);
        pProduct.setProductCode(productCode);
        pProduct.setProductName(productName);
        pProduct.setProductMainImage(productMainImage);
        pProduct.setSalePrice(salePrice);
        pProduct.setInventoryNum(inventoryNum);
        pProduct.setFreight(freight);
        pProduct.setProductDescription(productDescription);
        pProduct.setCreateOn(new Date());
        pProduct.setStatus(1);
        return mapper.insertSelective(pProduct);
    }

    @Override
    public int editProduct(String productId, String pCategoryCategoryId, String productName, String productMainImage, BigDecimal salePrice, Integer inventoryNum, BigDecimal freight, String productDescription) {
        PProduct pProduct = new PProduct();
        pProduct.setProductId(productId);
        pProduct.setpCategoryCategoryId(pCategoryCategoryId);
        pProduct.setProductName(productName);
        pProduct.setProductMainImage(productMainImage);
        pProduct.setSalePrice(salePrice);
        pProduct.setInventoryNum(inventoryNum);
        pProduct.setFreight(freight);
        pProduct.setProductDescription(productDescription);
        pProduct.setCreateOn(new Date());
        pProduct.setStatus(1);
        return mapper.updateByPrimaryKeySelective(pProduct);
    }
    @Override
    public List<PProduct> getPProductByShopIdAndProductName(Integer shopId, String productName,Integer status) {
        PProductExample example=new PProductExample();
        if (status==null) {
            if (StringUtils.isEmpty(productName) == false) {
                example.or().andWxShopShopIdEqualTo(shopId).andProductNameLike(productName);
            }
            if (StringUtils.isEmpty(productName) == true) {
                example.or().andWxShopShopIdEqualTo(shopId);
            }
        }else  if (status!=null){
                if (StringUtils.isEmpty(productName) == false){
                    example.or().andWxShopShopIdEqualTo(shopId).andProductNameLike(productName).andStatusEqualTo(status);
                }
                if (StringUtils.isEmpty(productName) == true){
                    example.or().andWxShopShopIdEqualTo(shopId).andStatusEqualTo(status);
                }
        }
        return mapper.selectByExample(example);
    }

    @Override
    public List<PProduct> pProductByPProductId(Integer shopId, String wxShopCategoryId,Integer status) {
        PProductExample example=new PProductExample();
        if (status==null){
            example.or().andWxShopShopIdEqualTo(shopId).andWxShopCategoryIdEqualTo(wxShopCategoryId);
        }else if (status!=null){

            example.or().andWxShopShopIdEqualTo(shopId).andWxShopCategoryIdEqualTo(wxShopCategoryId).andStatusEqualTo(status);
        }
        return mapper.selectByExample(example);
    }

}
